//
//  AEPointsMallTopView.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/13.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEPointsMallTopView.h"
#import "AEPointsMallViewModel.h"
#import "AE_Solar-Swift.h"

@interface AEPointsMallTopView ()

@property (nonatomic, strong) AEPointsMallViewModel *viewModel;

/// 我的积分 标题
@property (nonatomic, strong) UILabel *myScoreLabel;

/// 积分
@property (nonatomic, strong) UILabel *scoreNumLabel;

@property (nonatomic, strong) CALayer *vertOneLine;

@property (nonatomic, strong) CALayer *vertTwoLine;

/// 兑换商品
@property (nonatomic, strong) TYZImageTitleButton *btnExchangeGoods;

/// 历史积分
@property (nonatomic, strong) TYZImageTitleButton *btnHistoryPoints;

/// 我的兑换
@property (nonatomic, strong) TYZImageTitleButton *btnMyExchange;

@property (nonatomic, strong) UIImageView *moreCntImgBgView;

/// 获取更多积分 标题
@property (nonatomic, strong) UILabel *moreCntTitleLabel;

/// 更多
@property (nonatomic, strong) UIButton *btnMore;

/// 积分兑换
@property (nonatomic, strong) UILabel *bottomTitleLabel;


/// 我的积分 标题
- (void)initWithMyScoreLabel;

/// 积分
- (void)initWithScoreNumLabel;


/// 兑换商品
- (void)initWithBtnExchangeGoods;

/// 历史积分
- (void)initWithBtnHistoryPoints;

/// 我的兑换
- (void)initWithBtnMyExchange;


@end

@implementation AEPointsMallTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self initWithSubView];
    }
    return self;
}

- (void)initWithSubView {
    
    self.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    
    [self initWithVertOneLine];
    
    [self initWithVertTwoLine];
    
    /// 兑换商品
    [self initWithBtnExchangeGoods];

    /// 历史积分
    [self initWithBtnHistoryPoints];

    /// 我的兑换
    [self initWithBtnMyExchange];

    [self initWithMoreCntImgBgView];
    
    [self initWithMoreCntTitleLabel];
    [self initWithBtnMore];
    
    [self initWithBottomTitleLabel];
}


/// 我的积分 标题
- (void)initWithMyScoreLabel {
    if (!_myScoreLabel) {
        CGRect frame = CGRectMake(20, 10, self.width - 20 * 2, 16);
        _myScoreLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
    self.myScoreLabel.text = [AEStringExten localizedValueWithKey:@"我的积分"];
    if (self.viewModel && self.viewModel.userInfoModel) {
        
    }
}

/// 积分
- (void)initWithScoreNumLabel {
    if (!_scoreNumLabel) {
        CGRect frame = self.myScoreLabel.frame;
        frame.size.height = 46;
        frame.origin.y = self.myScoreLabel.bottom + 3;
        _scoreNumLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:33 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
    if (self.viewModel && self.viewModel.userInfoModel) {
        self.scoreNumLabel.text = objectNull(self.viewModel.userInfoModel.scoreNum);
    }
}

- (void)initWithVertOneLine {
    if (!_vertOneLine) {
        CGRect frame = CGRectMake(kScreenW / 3.0, 97, 1, 28);
        _vertOneLine = [CALayer drawLine:self frame:frame lineColor:[UIColor colorWithHexString:@"#EBEBEB"]];
    }
}

- (void)initWithVertTwoLine {
    if (!_vertTwoLine) {
        CGRect frame = self.vertOneLine.frame;
        frame.origin.x = self.vertOneLine.right + self.vertOneLine.left;
        _vertTwoLine = [CALayer drawLine:self frame:frame lineColor:[UIColor colorWithHexString:@"#EBEBEB"]];
    }
}

/// 兑换商品
- (void)initWithBtnExchangeGoods {
    if (!_btnExchangeGoods) {
        UIImage *image = [UIImage imageNamed:@"ic_exchange_goods"];
        CGRect frame = CGRectMake(20, 94, self.vertOneLine.left - 40, 38);
        NSString *title = [AEStringExten localizedValueWithKey:@"兑换商品"];
        _btnExchangeGoods = [TYZImageTitleButton createWithImageButton:self frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightMedium] target:self action:@selector(actionWithExchangeGoods:) propertiesBlock:^(TYZImageTitleButton * _Nonnull button) {
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
            [button setImage:image forState:UIControlStateNormal];
            [button layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleTop imageTitleSpace:4];
        }];
    }
}

/// 历史积分
- (void)initWithBtnHistoryPoints {
    if (!_btnHistoryPoints) {
        UIImage *image = [UIImage imageNamed:@"ic_history_points"];
        CGRect frame = self.btnExchangeGoods.frame;
        NSString *title = [AEStringExten localizedValueWithKey:@"历史积分"];
        _btnHistoryPoints = [TYZImageTitleButton createWithImageButton:self frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightMedium] target:self action:@selector(actionWithHistoryPoints:) propertiesBlock:^(TYZImageTitleButton * _Nonnull button) {
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
            [button setImage:image forState:UIControlStateNormal];
            [button layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleTop imageTitleSpace:4];
        }];
        _btnHistoryPoints.centerX = self.centerX;
    }
}

/// 我的兑换
- (void)initWithBtnMyExchange {
    if (!_btnMyExchange) {
        UIImage *image = [UIImage imageNamed:@"ic_my_exchange"];
        CGRect frame = self.btnExchangeGoods.frame;
        frame.origin.x = self.vertTwoLine.left + 20;
        NSString *title = [AEStringExten localizedValueWithKey:@"我的兑换"];
        _btnMyExchange = [TYZImageTitleButton createWithImageButton:self frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightMedium] target:self action:@selector(actionWithMyExchange:) propertiesBlock:^(TYZImageTitleButton * _Nonnull button) {
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
            [button setImage:image forState:UIControlStateNormal];
            [button layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleTop imageTitleSpace:4];
        }];
    }
}

- (void)initWithMoreCntImgBgView {
    if (!_moreCntImgBgView) {
        UIImage *image = [UIImage imageNamed:@"ic_points_root_more"];
        CGRect frame = CGRectMake(15, _btnMyExchange.bottom + 23, self.width - 30, image.size.height);
        _moreCntImgBgView = [UIImageView createWithImageView:self frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.userInteractionEnabled = YES;
        }];
    }
}

- (void)initWithMoreCntTitleLabel {
    if (!_moreCntTitleLabel) {
        CGRect frame = CGRectMake(15, 14, self.moreCntImgBgView.width - 30, 22);
        NSString *title = [AEStringExten localizedValueWithKey:@"获取更多积分"];
        _moreCntTitleLabel = [UILabel createWithLabel:self.moreCntImgBgView frame:frame font:[UIFont systemFontOfSize:15 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
            label.text = title;
        }];
    }
}

- (void)initWithBtnMore {
    if (!_btnMore) {
        NSString *title = [AEStringExten localizedValueWithKey:@"点击获取更多积分"];
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
        float width = [title widthForFont:font] + 20;
        CGRect frame = CGRectMake(15, self.moreCntTitleLabel.bottom + 11, width, 24);
        _btnMore = [UIButton createWithButton:self.moreCntImgBgView frame:frame font:font target:self action:@selector(actionWithMore:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
            button.backgroundColor = [UIColor colorWithHexString:@"#F65602"];
            button.layer.cornerRadius = frame.size.height * 0.5;
            button.layer.masksToBounds = YES;
        }];
    }
}

- (void)initWithBottomTitleLabel {
    if (!_bottomTitleLabel) {
        NSString *title = [AEStringExten localizedValueWithKey:@"积分兑换"];
        CGRect frame = CGRectMake(15, self.moreCntImgBgView.bottom + 15, self.width - 30, 26);
        _bottomTitleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:19 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
            label.text = title;
        }];
    }
}

/// 点击获取更多积分
/// - Parameter sender: sender description
- (void)actionWithMore:(id)sender {
    if (self.touchMorePointsBlock) {
        self.touchMorePointsBlock();
    }
}

/// 兑换商品
/// - Parameter sender: sender description
- (void)actionWithExchangeGoods:(id)sender {
    if (self.touchExchangeGoodsBlock) {
        self.touchExchangeGoodsBlock();
    }
}

/// 历史积分
/// - Parameter sender: sender description
- (void)actionWithHistoryPoints:(id)sender {
    if (self.touchHistoryPointsBlock) {
        self.touchHistoryPointsBlock();
    }
}

/// 我的兑换
/// - Parameter sender: sender description
- (void)actionWithMyExchange:(id)sender {
    if (self.touchMyExchangeBlock) {
        self.touchMyExchangeBlock();
    }
}


- (void)updateWithViewData:(id)data {
    
    self.viewModel = data;
    
    /// 我的积分 标题
    [self initWithMyScoreLabel];

    /// 积分
    [self initWithScoreNumLabel];

}

@end
